package com.wjk.easy.adapter;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import com.wjk.easy.R;
import com.wjk.easy.db.Exam;
import com.wjk.easy.db.ExamData;
import com.wjk.easy.db.LastUserBean;
import com.wjk.easy.utils.CommonUtil;
import com.wjk.easy.utils.D;

import org.litepal.LitePal;
import org.litepal.crud.async.UpdateOrDeleteExecutor;
import org.litepal.crud.callback.UpdateOrDeleteCallback;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class ExamManageListAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Exam> list;

    public ExamManageListAdapter(Context context, List<Exam> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_exam, parent,
                false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        //TextView tv_exam_name, tv_group_count,tv_create_time,tv_delete_data,tv_remark;
        final Exam exam = list.get(position);
        MyViewHolder h = (MyViewHolder) holder;
        h.tv_exam_name.setText(exam.getName());
        h.tv_group_count.setText(exam.getGroups()+"个");
        h.tv_create_time.setText(CommonUtil.dateToStr(exam.getCreateTime()));
        h.tv_remark.setText(exam.getRemark());

       /* h.ll_exam_container.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                    return true;
            }
        });*/

        if(position%2==0){
            h.ll_exam_container.setBackgroundColor(context.getResources().getColor(R.color.trans));
        }else{
            h.ll_exam_container.setBackgroundColor(context.getResources().getColor(R.color.colorSelectDarkTrans));
        }

        h.tv_delete_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context)
                        .setTitle("删除实验")
                        .setMessage("确定要删除" + exam.getName() + "吗？与之关联的所有数据都会被删除")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                removeData(position);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
            }
        });
    }

    public void setEmptyListener(EmptyListener emptyListener){
        this.emptyListener = emptyListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //  添加数据
    public void addData(int position, Exam data) {
        list.add(position, data);
        //添加动画
        notifyItemInserted(position);
    }

    //  删除数据
    public void removeData(final int position) {
        LastUserBean currentUser = D.queryCurrentUser((Activity) context);
        if(currentUser!=null){
            int examId = list.get(position).getId();
            final int i = LitePal.deleteAll(ExamData.class, "examid=? and userid=?", examId + "", currentUser.getUserId() + "");
            UpdateOrDeleteExecutor executor = LitePal.deleteAllAsync(Exam.class, "id=? and userid=?" , examId+"",currentUser.getUserId()+"");
            executor.listen(new UpdateOrDeleteCallback() {
                @Override
                public void onFinish(int rowsAffected) {
                    if(rowsAffected>0){
                        list.remove(position);
                        //删除动画
                        notifyItemRemoved(position);
                        notifyDataSetChanged();
                        if(list.size()<1&&emptyListener!=null){
                            emptyListener.onEmpty();
                        }
                        Toast.makeText(context,"删除成功,同时删除"+i+"条数据", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(context,"删除失败", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
    public void setDataList(List<Exam> examList) {
        list.clear();
        list.addAll(examList);
        notifyDataSetChanged();
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_exam_name, tv_group_count, tv_create_time, tv_delete_data, tv_remark;
        LinearLayout ll_exam_container;

        MyViewHolder(View view) {
            super(view);
            tv_exam_name = view.findViewById(R.id.tv_exam_name);
            tv_group_count = view.findViewById(R.id.tv_group_count);
            tv_create_time = view.findViewById(R.id.tv_create_time);
            tv_delete_data = view.findViewById(R.id.tv_delete_data);
            tv_remark = view.findViewById(R.id.tv_remark);
            ll_exam_container = view.findViewById(R.id.ll_exam_container);
        }
    }
    EmptyListener emptyListener;
    public interface EmptyListener{
        void onEmpty();
        void onExportClick(Exam exam);
    }
}
